package com.onemile.controls;

import java.util.ArrayList;

import com.onemile.app.R;
import com.onemile.app.UIShopDetails;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.entity.model.ECommodity;
import com.onemile.entity.model.EShopInfo;
import com.onemile.utils.UtiDialogsFactory;

import android.R.string;
import android.content.Context;
import android.content.Intent;
import android.sax.Element;
import android.test.UiThreadTest;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class AdpLV_ShopList extends BaseAdapter
{
   private ArrayList<EShopInfo> mList;
   private LayoutInflater mInflater;
   private Context mContext;

   public AdpLV_ShopList(Context context)
   {
	  mList = new ArrayList<EShopInfo>();
	  mContext=context;
	  mInflater = LayoutInflater.from(context);
   }

   public void addItem(ArrayList<EShopInfo> _list)
   {
	  if (mList != null)
	  {
		 for (EShopInfo eShop : _list)
		 {
			mList.add(eShop);
		 }
	  }
   }
   
   @Override
   public int getCount()
   {
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int arg0)
   {
	  return 0;
   }

   @Override
   public View getView(final int position, View currentView, ViewGroup arg2)
   {
	  try
	  {
		 ViewHolder holder;
		 if (currentView == null)
		 {
			holder = new ViewHolder();
			currentView = mInflater.inflate(R.layout.shoplist_item, null);
			holder.ShopName = (TextView) currentView.findViewById(R.id.txtShopName_shoplist_item);
			holder.AvgPrice = (TextView) currentView.findViewById(R.id.txtAvgPrice_shoplist_item);
			holder.ShopCategory = (TextView) currentView.findViewById(R.id.txtShopCategory_shoplist_item);
			holder.ShopDescription = (TextView) currentView.findViewById(R.id.txtShopDescription_shoplist_item);
			holder.ShopDistance = (TextView) currentView.findViewById(R.id.txtDistance_shoplist_item);
			holder.StarBar = (RatingBar) currentView.findViewById(R.id.rabScore_shoplist_item);
			holder.ImgQuan = (ImageView) currentView.findViewById(R.id.imgQuan_shoplist_item);
			holder.imgShopDetails=(ImageView)currentView.findViewById(R.id.imgShopDetails_shoplist_item);
			currentView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) currentView.getTag();
		 }
		 if(mList.get(position).getCouponNum()==0)
		 {
			holder.ImgQuan.setVisibility(View.GONE);
		 }
		 else 
		 {
			holder.ImgQuan.setVisibility(View.VISIBLE);
		 }
		 Float star=Float.valueOf(mList.get(position).getShopsPraise())/(Integer.valueOf(mList.get(position).getShopsPoor())+Integer.valueOf(mList.get(position).getShopsPraise()));
		 holder.ShopName.setText(mList.get(position).getShopsName());
		 holder.AvgPrice.setText(String.format("%s%s%s","人均",mList.get(position).getConsumption(),"元"));
		 holder.ShopCategory.setText(String.format("%s%s%s","【",mList.get(position).getShopsType(),"】"));
		 holder.ShopDistance.setText(" "+mList.get(position).getDistance()+"m");
		 holder.StarBar.setRating(4);
		 holder.imgShopDetails.setOnClickListener(new View.OnClickListener()
		 {
		    @Override
		    public void onClick(View v)
		    {
//		       Session.getInstance().setCurrentShop(mList.get(position));
               Intent intent=new Intent();
               intent.setClass(mContext, UIShopDetails.class);
               intent.putExtra(PubConst.Intent_Key.SHOP_ID, mList.get(position).getShopsId());
               mContext.startActivity(intent);
            }
		 });
		 holder.ShopDescription.setText(mList.get(position).getRecommendedDishes());
		 return currentView;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "AdpLV_ShopList_getView(position, currentView, arg2) ", e);
		 }
	  }
	  return new View(mContext);
   }

   static class ViewHolder
   {
	  TextView ShopName;
	  TextView AvgPrice;
	  TextView ShopCategory;
	  TextView ShopDescription;
	  TextView ShopDistance;
	  RatingBar StarBar;
	  ImageView ImgQuan;
	  ImageView imgShopDetails;
   }

}
